/*
 * Created on 2004-8-3
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */

package cn.com.yinhex.as.util;

/**
 * @author test
 * 
 *         TODO To change the template for this generated type comment go to
 *         Window - Preferences - Java - Code Generation - Code and Comments
 */
public class DOC2XML {
	public String toString(DOC[] docs) {
		if (docs == null) {
			return "";
		}
		StringBuffer sb = new StringBuffer();
		sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
		sb.append("<docs author=\"ayou\">\n");
		for (int i = 0; i < docs.length; i++) {
			if (docs[i] != null) {
				sb.append("    <doc id=\"" + i + "\">\n");
				String[] keys = docs[i].getKeys();
				for (int j = 0; j < keys.length; j++) {
					try {
						sb.append("        <" + keys[j] + "><![CDATA[" + docs[i].get(keys[j]) + "]]></" + keys[j] + ">\n");
					} catch (Exception e) {
					}
				}
				sb.append("    </doc>\n");
			}
		}
		sb.append("</docs>\n");
		return sb.toString();
	}

	public void write(String filename, DOC[] docs) {
		FileUtil.write(filename, toString(docs));
	}
}
